<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>UserGuide - Compiler directives (for different behavior on different OS)</title></head>
<body bgcolor="#FFFFDF" link="#009999" vlink="#006666" alink="#006666">
<font face="Arial" size="2"><p align="center"><b><font size="5">UserGuide - Compiler directives (for different behavior on different OS)</font></b></p>



This will be our last visit to the File Properties program. There is one limitation 
discussed previously to overcome and we've left it until now because it is a special case. 
<br>
<br>
So far the Attributes column on the display has simply been an <a href="../reference/variables.html">integer</a>. 
This is because the return values of the <a href="../filesystem/getfileattributes.html">GetFileAttributes()</a> and <a href="../filesystem/directoryentryattributes.html">DirectoryEntryAttributes()</a> 
instructions have a different meaning on Windows systems compared with Mac and Linux systems. 
<br>
<br>
We can't allow for this difference at run-time, however we can use 
<a href="../reference/compilerdirectives.html">Compiler Directives</a> to have the program behave 
differently on the three different operating systems. 
<br>
<br>

Add this new procedure declaration to that section. 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#006666">Declare</font></b>.s <font color="#006666">AttributeString</font>(Attributes.l)
</font></pre>



Add this new procedure to the implementation section. 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#006666">Procedure</font></b>.s <font color="#006666">AttributeString</font>(Attributes.l)
    <font color="#006666">; Converts an integer attributes value into a string description.</font>
    <font color="#006666">; Supports Linux, Mac and Windows system's attributes.</font>
    
    <b><font color="#006666">Protected</font></b>.s Result
    
    Result = ""
    
    <b><font color="#006666">CompilerIf</font></b> <font color="#924B72">#PB_Compiler_OS</font> = <font color="#924B72">#PB_OS_Windows</font>
      ��
      ��<font color="#006666">; These are the attributes for Windows systems.</font>
      ��<font color="#006666">; A logical-and of the attribute with each constant filters out that bit and can then be used for comparison.</font>
      
      ��<b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_Archive</font>
        ���Result + "A"
      ��<b><font color="#006666">Else</font></b>
        ���Result + " "
      ��<b><font color="#006666">EndIf</font></b>
      ��
      ��<b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_Compressed</font>
        ���Result + "C"
      ��<b><font color="#006666">Else</font></b>
        ���Result + " "
      ��<b><font color="#006666">EndIf</font></b>
      ��
      ��<b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_Hidden</font>
        ���Result + "H"
      ��<b><font color="#006666">Else</font></b>
        ���Result + " "
      ��<b><font color="#006666">EndIf</font></b>
      ��
      ��<b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ReadOnly</font>
        ���Result + "R"
      ��<b><font color="#006666">Else</font></b>
        ���Result + " "
      ��<b><font color="#006666">EndIf</font></b>
      ��
      ��<b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_System</font>
        ���Result + "S"
      ��<b><font color="#006666">Else</font></b>
        ���Result + " "
      ��<b><font color="#006666">EndIf</font></b>
      ��
    <b><font color="#006666">CompilerElse</font></b>
      
      <font color="#006666">; These are the attributes for Mac and Linux systems.</font>
      
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_Link</font>
        Result + "L "
      <b><font color="#006666">Else</font></b>
        Result + "�"
      <b><font color="#006666">EndIf</font></b>
      
      <font color="#006666">; User attributes.</font>
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ReadUser</font>
        Result + "R"
      <b><font color="#006666">Else</font></b>
        Result + " "
      <b><font color="#006666">EndIf</font></b>
      
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_WriteUser</font>
        Result + "W"
      <b><font color="#006666">Else</font></b>
        Result + " "
      <b><font color="#006666">EndIf</font></b>
      
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ExecUser</font>
        Result + "X "
      <b><font color="#006666">Else</font></b>
        Result + "�"
      <b><font color="#006666">EndIf</font></b>
      
      <font color="#006666">; Group attributes.</font>
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ReadGroup</font>
        Result + "R"
      <b><font color="#006666">Else</font></b>
        Result + " "
      <b><font color="#006666">EndIf</font></b>
      
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_WriteGroup</font>
        Result + "W"
      <b><font color="#006666">Else</font></b>
        Result + " "
      <b><font color="#006666">EndIf</font></b>
      
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ExecGroup</font>
        Result + "X "
      <b><font color="#006666">Else</font></b>
        Result + "�"
      <b><font color="#006666">EndIf</font></b>
      
      <font color="#006666">; All attributes.</font>
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ReadAll</font>
        Result + "R"
      <b><font color="#006666">Else</font></b>
        Result + " "
      <b><font color="#006666">EndIf</font></b>
      
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_WriteAll</font>
        Result + "W"
      <b><font color="#006666">Else</font></b>
        Result + " "
      <b><font color="#006666">EndIf</font></b>
      
      <b><font color="#006666">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ExecAll</font>
        Result + "X"
      <b><font color="#006666">Else</font></b>
        Result + " "
      <b><font color="#006666">EndIf</font></b>
      
    <b><font color="#006666">CompilerEndIf</font></b>
    
    <font color="#006666">; Return the result.</font>
    <b><font color="#006666">ProcedureReturn</font></b> Result
    
  <b><font color="#006666">EndProcedure</font></b>
</font></pre>



Finally, replace these two lines in the ListLoad procedure 
 
<pre><font face="Courier New, Courier, mono"size="2">      <font color="#006666">; Convert the attributes to a string, for now.</font>
      Attrib =<font color="#006666"> StrU</font>(<font color="#006666">Files</font>()\Attributes)
</font></pre>



with these, 
 
<pre><font face="Courier New, Courier, mono"size="2">      <font color="#006666">; Call AttributeString to convert the attributes to a string representation.</font>
      Attrib =<font color="#006666"> AttributeString</font>(<font color="#006666">Files</font>()\Attributes)
</font></pre>



Now when the program is executed a string display will be shown instead of the integer values. 
On a Windows system it would look something like this (assuming all attributes are set): 
 
<pre><font face="Courier New, Courier, mono"size="2">  ACHRS
</font></pre>



and on the other two systems: 
 
<pre><font face="Courier New, Courier, mono"size="2">  L RWX RWX RWX
</font></pre>



The <a href="../reference/compilerdirectives.html">CompilerIf</a> instruction works much like an 
<a href="../reference/if_endif.html">If</a> instruction - however it is the compiler that makes the 
decision at compile-time, rather than the executable at run-time. This means that we can 
include different code to handle the file attributes on the different operating systems. 
<br>
<br>
The lines between: 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#006666">CompilerIf</font></b> <font color="#924B72">#PB_Compiler_OS</font> = <font color="#924B72">#PB_OS_Windows</font>
</font></pre>



and 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#006666">CompilerElse</font></b>
</font></pre>



will be compiled on Windows systems. The constant #PB_Compiler_OS is automatically defined 
by PureBasic to allow this kind of program logic. 
<br>
<br>
The other section will be used on Mac and Linux systems - which work the same way, 
conveniently. If these operating systems had different attribute values too, then we could 
use <a href="../reference/compilerdirectives.html">CompilerSelect</a> and <a href="../reference/compilerdirectives.html">CompilerCase</a> 
to make a three-way determination. 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#006666">CompilerSelect</font></b> <font color="#924B72">#PB_Compiler_OS</font>
      
    <b><font color="#006666">CompilerCase</font></b> <font color="#924B72">#PB_OS_Linux</font>
      <font color="#006666">; Code for Linux systems.</font>
      

    <b><font color="#006666">CompilerCase</font></b> <font color="#924B72">#PB_OS_MacOS</font>
      <font color="#006666">; Code for Mac systems.</font>
      
    <b><font color="#006666">CompilerCase</font></b> <font color="#924B72">#PB_OS_Windows</font>
      <font color="#006666">; Code for Windows systems.</font>
      
  <b><font color="#006666">CompilerEndSelect</font></b>
</font></pre>



The last compiler directive that we're going to discuss here is: 
<a href="../reference/compilerdirectives.html">EnableExplicit</a>. 
<br>
<br>
There is a good reason for using this directive. It requires that all <a href="../reference/variables.html">variables</a> 
must be defined explicitly before usage, in some way, (using <a href="../reference/define.html">Define</a>, 
<a href="../reference/dim.html">Dim</a>, <a href="../reference/global.html">Global</a>, <a href="../reference/protected.html">Protected</a>, 
<a href="../reference/static.html">Static</a> etc.) Doing so eliminates the possibility of logic errors due 
to mistyped variable names being defined "on-the-fly". This type of subtle error will not affect 
a program's compilation but could well present as an inconvenient bug at run-time. Using this 
directive eliminates this kind of problem as a compiler error would occur. 
<br>
<br>
For example: 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#006666">EnableExplicit</font></b>
  
  <b><font color="#006666">Define</font></b>.l Field, FieldMax
  
  <font color="#006666">; ...</font>
  
  <b><font color="#006666">If</font></b> Field &lt; FieldMax
    <font color="#006666">; If EnableExplicit is omitted this section of code may not execute when intended because FieldMax will be zero.</font>
  <b><font color="#006666">EndIf</font></b>
</font></pre>


<p><b>UserGuide Navigation</b></p><blockquote>

<a href="../reference/ug_procedures.html">&lt; Previous: Structuring code in Procedures</a> | <a href="../reference/ug_overview.html">Overview</a> | <a href="../reference/ug_files.html">Next: Reading and writing files &gt;</a> 
</body></html>